package cn.swenty.backstage.framework.taglib;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.swenty.backstage.framework.common.util.StringUtil;
import cn.swenty.backstage.framework.database.annotation.TableSeg;
import cn.swenty.backstage.framework.database.entity.Entity;
import cn.swenty.backstage.framework.util.JsonUtils;
import net.sf.json.JSONObject;

public class EntitySelectTag  extends SimpleTag {
	
	
	private String first;
	
	
	private String entity;
	
	private String filter;
	
	private String key_;
	
	private String value_;
	
	private String selectValue;
	
	private String selectText;
	
	
	public String getEntity() {
		return entity;
	}

	public void setEntity(String entity) {
		this.entity = entity;
	}

	public String getFilter() {
		return filter;
	}

	public void setFilter(String filter) {
		this.filter = filter;
	}

	public String getSelectText() {
		return selectText;
	}

	public void setSelectText(String selectText) {
		this.selectText = selectText;
	}

	public String getSelectValue() {
		return selectValue;
	}

	public void setSelectValue(String selectValue) {
		this.selectValue = selectValue;
	}


	public String getKey_() {
		return key_;
	}

	public void setKey_(String key_) {
		this.key_ = key_;
	}

	public String getValue_() {
		return value_;
	}

	public void setValue_(String value_) {
		this.value_ = value_;
	}

	public String getFirst() {
		return first;
	}

	public void setFirst(String first) {
		this.first = first;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected String getPrintStr() throws Exception {
		
		StringBuffer sb = new StringBuffer();
		
		this.addLine(sb, "<select ");
		if(StringUtil.isNotNull(this.onclick)){
			this.addLine(sb, " onclick="+this.onclick+" ");
		}
		if(StringUtil.isNotNull(this.name)){
			this.addLine(sb, " name='"+this.name+"' ");
		}
		if(StringUtil.isNotNull(this.id)){
			this.addLine(sb, " id='"+this.id+"' ");
		}
		if(StringUtil.isNotNull(this.onchange)){
			this.addLine(sb, " onchange="+this.onchange+" ");
		}
		if(StringUtil.isNotNull(this.className)){
			this.addLine(sb, " class='"+this.className+"' ");
		}
		if(StringUtil.isNotNull(this.style)){
			this.addLine(sb, " style='"+this.style+"' ");
		}
		if(StringUtil.isNotNull(this.onfocus)){
			this.addLine(sb, " onfocus="+this.onfocus+" ");
		}
		if(StringUtil.isNotNull(this.onblur)){
			this.addLine(sb, " onblur="+this.onblur+" ");
		}
		if(StringUtil.isNotNull(this.validate)){
			if(JsonUtils.isJsonObject(this.validate)){
				Map<String, Object> map = JsonUtils.parseJSONMap(this.validate);
				Iterator<String> it = map.keySet().iterator();
				while (it.hasNext()){
					String key = it.next();
					Object value = map.get(key);
					this.addLine(sb, " "+key + (StringUtil.isNull(value)?" ":(" = "+value+" ")));
				}
			}else{
				this.addLine(sb, " validate="+this.validate+" ");
			}
		
		}
		this.addLine(sb, " >");
		
		if(StringUtil.isNotNull(first)){
			this.addLine(sb, "<option value=''>"+first+"</option>");
		}
		List lists =  null;
		Class c= Class.forName(entity);
		TableSeg ts = (TableSeg) c.getAnnotation(TableSeg.class);
		Object o =  c.newInstance();
		if(o instanceof Entity && ts != null){
			if(StringUtil.isNotNull(filter)){
				Map<String, Object>  map = JsonUtils.parseJSONMap(filter);
				((Entity)o).putAll(map);
			}
			lists =  ((Entity)o).query();
		}
		if(lists!=null){
			for(int i=0;i<lists.size();i++){
				JSONObject object = JSONObject.fromObject(lists.get(i));
				Object key = object.get(key_.toLowerCase());
				Object value = object.get(value_.toLowerCase());
				if(StringUtil.isNotNull(selectValue) && selectValue.equals(key)){
					this.addLine(sb, "<option value='"+(key==null?"":key)+"' selected>"+(value==null?"":value)+"</option>");
				}else if(StringUtil.isNotNull(selectText) && selectText.equals(value)){
					this.addLine(sb, "<option value='"+(key==null?"":key)+"' selected>"+(value==null?"":value)+"</option>");
				}else{
					this.addLine(sb, "<option value='"+(key==null?"":key)+"'>"+(value==null?"":value)+"</option>");
				}
				
			}
		}
		
		this.addLine(sb, "</select>");
		return sb.toString();
	}

}
